package com.jrx.anytxn.reporting.job.unionFile.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.job.unionFile.step.InterestFileListener;
import com.jrx.anytxn.reporting.job.unionFile.step.InterestFileProcessor;
import com.jrx.anytxn.reporting.job.unionFile.step.InterestFileReader;
import com.jrx.anytxn.reporting.job.unionFile.step.InterestFileWriter;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.TlInterestAccrualLog;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisPagingItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * 生成 计提流水文件(增量 计提日志表抽取)
 * 只统计了利息，罚息。复利是否需要上送？
 * lbz
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class GenerateAccruedSerialFileConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private InterestFileProcessor interestFileProcessor;
    @Autowired
    private InterestFileWriter interestFileWriter;
    @Autowired
    private InterestFileListener interestFileListener;

    @Bean
    public Job generateAccruedSerialFileJob() {
        return jobBuilderFactory.get("generateAccruedSerialFileJob").incrementer(new RunIdIncrementer()).listener(interestFileListener)
                .start(generateInterestFileStep())
                .build();
    }

    @Bean
    public Step generateInterestFileStep(){
        return stepBuilderFactory.get("generateInterestFileStep")
                .<TlInterestAccrualLog, String>chunk(anyTxnBatchProperties.getFileConfig().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(interestFileReader())
                .processor(interestFileProcessor)
                .writer(interestFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getFileConfig().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<TlInterestAccrualLog> interestFileReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        return new InterestFileReader(sqlSessionFactory, org,anyTxnBatchProperties.getFileConfig().getPageSize());
    }


}
